<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+


class KBClientView_index extends KBClientView_common
{	

	var $num_subcategories = 0;

	
	function &execute(&$manager) {
		
		
		if(!$this->category_id) {
			$rows = &$this->stripVars($manager->getCategoryList($this->top_parent_id));
			$title = $this->msg['category_title_msg'];
			$meta_title = '';
			
		} else {
			$rows = &$this->stripVars($manager->getCategoryList($this->category_id));
			$title =  $this->msg['subcategory_title_msg'];
			$meta_title = $manager->categories[$this->category_id]['name'];
			$this->num_subcategories = count($rows);
		}
		

		$this->meta_title = $meta_title;

		$data = array();
		$data[] = &$this->getTop($manager);
		$data[] = &$this->getCategoryList($rows, $title, $manager);
		$data[] = &$this->getEntryList($manager);	
		$data = implode('', $data);
		
		return $data;
	}
	

/*
	function getEntriesNumFormated($num_entry_format, $num_entries, $num_categories = false) {
		
		$num_entries = ($num_entries) ? $num_entries : 0;
		
		$str[0] = "";
		$str[1] = "(%s)";
		$str[2] = (!$num_categories) ? $str[1] : "(%s / %s)";
		
		return sprintf($str[$num_entry_format], $num_entries, $num_categories);
	}
*/
	
	
	function &getCategoryList($rows, $title, $manager) {
		
		$num_category_td = $manager->getSetting('num_category_cols');
		if(!$rows || !$this->display_categories || $num_category_td == 0) { 
			$empty = '';
			return $empty; 
		}
		
		if($manager->getSetting('show_num_entries')) {
			$count_entries = $manager->countEntriesPerCategory($manager->getValuesString($rows));
		}
		
		$num = count($rows);
		$rows = array_chunk($rows, $num_category_td);
		
		// td width
		if($num < $num_category_td) { $td_width = round(100/$num); } 
		else                        { $td_width = round(100/$num_category_td); }
		
		
		$tpl = &new tplTemplatez($this->template_dir . 'category_list.html');
		
		foreach($rows as $k => $v) {
			foreach($v as $k1 => $v1) {
				
				$v1['td_width']	= $td_width;
				$v1['num_entries'] = (!empty($count_entries[$v1['id']])) ? sprintf('(%s)', $count_entries[$v1['id']]) : '';
				$v1['category_link'] = $this->_getLink('index', $v1['id']);
				$v1['item_img'] = $this->_getItemImg($manager->is_registered, $v1['private'], true);
				
				$tpl->tplParse($v1, 'row_tr/row_td'); // parse nested
			}
		
			// do it nested
			$tpl->tplSetNested('row_tr/row_td');
			$tpl->tplParse('', 'row_tr');	
		}
		
		$tpl->tplAssign('title_msg', $title);
		$tpl->tplAssign('title_colspan', $num_category_td*2);
		$tpl->tplAssign($this->msg);
		$tpl->tplParse();
		
		return $tpl->tplPrint(1);
	}
	
	
	function &getEntryList(&$manager) {
		
		$manager->setSqlParams('AND ' . $manager->getPrivateSql(false));
		$manager->setSqlParams('AND ' . $manager->getCategoryRolesSql(false));
		
		// category articles
		if($this->category_id) {

			$num = $manager->getSetting('num_entries_per_page');
			return $this->_getCategoryEntries($manager, $num);
		
		// top articles
		} else {
			
			$most_num   = $manager->getSetting('num_most_viewed_entries');
			$recent_num = $manager->getSetting('num_recently_posted_entries');
			
			if($most_num && $recent_num) {
				$tpl = &new tplTemplatez($this->template_dir . 'top_article_format.html');
				
				$data = array();
				$data['data1'] = &$this->_getMostViewed($manager, $most_num);
				$data['data2'] = &$this->_getRecentlyPosted($manager, $recent_num);
				
				$tpl->tplParse($data);			
				return $tpl->tplPrint(1);				
			
			} elseif($most_num) {
				return $this->_getMostViewed($manager, $most_num);
				
			} elseif($recent_num) {
				return $this->_getRecentlyPosted($manager, $recent_num);
			}
		}
	}
	
	
	// parse data with articles
	function &_parseArticleList(&$manager, &$rows, $title, $by_page = '') {
		
		if(!$rows) {
			$empty = '';
			return $empty;
		}
		
		$tpl = &new tplTemplatez($this->template_dir . 'article_list.html');

/*
		$article_staff_padding = '1';
		$article_description_padding = 3;
		if($manager->getSetting('preview_article_limit') == 0) { 
			$article_description_padding = 0; 
			$article_staff_padding = 0;
		}
		$tpl->tplAssign('article_description_padding', $article_description_padding);
*/
		// what date to display
		$date = 'ts_updated';
		if($by_page && strpos($manager->getSetting('entry_sort_order'), 'added') !== false) {
			$date = 'ts_posted';
		}
		
		foreach($rows as $k => $row) {
			
			$private = ($row['private'] || $row['category_private']);
			$tpl->tplAssign('item_img', $this->_getItemImg($manager->is_registered, $private));
			$tpl->tplAssign('updated_date', $this->getFormatedDate($row[$date]));
			$tpl->tplAssign('entry_link', $this->_getLink('entry', $row['category_id'], $row['id']));
			//$tpl->tplAssign('comments_link', $this->_getLink('comments', $row['category_id'], $row['id']));
			
			$row['body'] = DocumentParser::getSummary($row['body'], $manager->getSetting('preview_article_limit'));
			
			if($manager->getSetting('preview_show_date')) {
				$tpl->tplSetNeeded('row/show_date');
				$article_staff_padding = '3';
			}				
			
			if($manager->getSetting('preview_show_hits')) {
				$tpl->tplSetNeeded('row/show_hits');
				$article_staff_padding = '3';
			}
			
			if($this->isRatingable($manager, $row['ratingable'])) {
				if($manager->getSetting('preview_show_rating')) {
					$tpl->tplSetNeeded('row/show_rate');
					$tpl->tplAssign('rating', $this->_getRating($row['rating']));					
					$article_staff_padding = '3';
				}
			}
			
			if($this->isCommentable($manager, $row['commentable'])) {
				if($manager->getSetting('preview_show_comments')) {
					$tpl->tplSetNeeded('row/show_comments');
					$article_staff_padding = '3';
				}				
			}			
			
			$tpl->tplAssign('article_staff_padding', $article_staff_padding);
			$tpl->tplParse(array_merge($row, $this->msg), 'row');
		} 
		
		//show more link if no categories
		//if(!$this->display_categories) {
		//	$tpl->tplAssign('more_link', $this->_getLink($row['category_id']));
		//	$tpl->tplAssign('_more_msg', 'more');
		//}
		
		$tpl->tplAssign('page_by_page', $by_page);
		$tpl->tplAssign('entry_title_msg', $title);
		$tpl->tplParse();
		
		return $tpl->tplPrint(1);
	}		
	
	
	function &_getCategoryEntries($manager, $num) {
			
		$manager->setSqlParams("AND cat.id = {$this->category_id}");
		$bp = $this->pageByPage($num, $manager->getEntryCountSql());
		
		$sort = array('name'         => 'e.title',
		              'sort_order'   => 'e.sort_order',
					  'added_desc'   => 'e.date_posted DESC',
					  'added_asc'    => 'e.date_posted ASC',
					  'updated_desc' => 'e.date_updated DESC',
					  'updated_asc'  => 'e.date_updated ASC',
					  'hits_desc'    => 'e.hits DESC',
					  'hits_asc'     => 'e.hits ASC');
		$sort = $sort[$manager->getSetting('entry_sort_order')];
		$manager->setSqlParamsOrder('ORDER BY ' . $sort);
		$rows = &$manager->getEntryList($bp->limit, $bp->offset);
		
		if(!$rows && !$this->num_subcategories) {
			$msg = &$this->getActionMsg('success', 'no_category_articles');
			return $msg;
		}
		
		return $this->_parseArticleList($manager, $this->stripVars($rows), $this->msg['entry_title_msg'], $bp->shortNavigate());
	}
	
	
	function &_getMostViewed($manager, $num) {
		//$manager->setSqlParamsOrder('ORDER BY hits DESC, e_to_cat.is_main');
		$manager->setSqlParams('AND e_to_cat.is_main = 1');
		$manager->setSqlParamsOrder('ORDER BY hits DESC');		
		$rows = &$manager->getEntryList($num, 0);
		return $this->_parseArticleList($manager, $this->stripVars($rows), $this->msg['most_viewed_entries_title_msg']);
	}
	
	
	function &_getRecentlyPosted($manager, $num) {
		//$manager->setSqlParamsOrder('ORDER BY date_posted DESC, e_to_cat.is_main');
		$manager->setSqlParams('AND e_to_cat.is_main = 1');
		$manager->setSqlParamsOrder('ORDER BY date_posted DESC');		
		$rows = &$manager->getEntryList($num, 0);
		return $this->_parseArticleList($manager, $this->stripVars($rows), $this->msg['recently_posted_entries_title_msg']);
	}	
}
?>